﻿#include "camera_qdialog.h"
//#include "hk_camera_plugin.h"
#include "qpluginloader.h"
#include "camera_dh.h"
typedef CameraInterface* (*Fuction)();

CameraBaseQDialog::CameraBaseQDialog(QDialog *parent, QSettings* qsettings,QString section) : QDialog(parent)
{
    move(0,0);
    setFixedSize(1920,1000);
//    this->setModal(true);
    QGridLayout *qgridLayout = new QGridLayout(this);


    QVBoxLayout *qgridLayout_left = new QVBoxLayout();

	m_qsettings = qsettings;
	m_section = section;


    QSize control_size(120,40);

//    m_qlabel_exposure_time = new QLabel();
//    m_qlabel_exposure_time->setText("曝光时长");
//    m_qlabel_exposure_time->setFixedSize(control_size);
//    qgridLayout_left->addWidget(m_qlabel_exposure_time,0,0);

//    m_qspinbox_exposure_time = new QSpinBox();
//    m_qspinbox_exposure_time->setFixedSize(control_size);
//    m_qspinbox_exposure_time->setMaximum(100000);
//    qgridLayout_left->addWidget(m_qspinbox_exposure_time,1,0);

//    m_qlabel_gain = new QLabel();
//    m_qlabel_gain->setText("增益");
//    m_qlabel_gain->setFixedSize(control_size);
//    qgridLayout_left->addWidget(m_qlabel_gain,2,0);

//    m_qspinbox_gain = new QSpinBox();
//    m_qspinbox_gain->setFixedSize(control_size);
//    m_qspinbox_gain->setMinimum(0);
//    m_qspinbox_gain->setMaximum(50);
//    qgridLayout_left->addWidget(m_qspinbox_gain,3,0);

//    m_qlabel_trigger_mode = new QLabel();
//    m_qlabel_trigger_mode->setText("相机触发模式");
//    m_qlabel_trigger_mode->setFixedSize(control_size);
//    qgridLayout_left->addWidget(m_qlabel_trigger_mode,4,0);

//    m_qcombox_trigger_mode = new QComboBox();
//    m_qcombox_trigger_mode->setFixedSize(control_size);
//    m_qcombox_trigger_mode->addItem("Off");
//    m_qcombox_trigger_mode->addItem("On");
//    qgridLayout_left->addWidget(m_qcombox_trigger_mode,5,0);

    m_qlabel_message = new QLabel();
    m_qlabel_message->setFixedSize(240,40);
    qgridLayout_left->addWidget(m_qlabel_message,1,0);


    m_qlabel_trigger_source = new QLabel();
    m_qlabel_trigger_source->setText("相机触发源");
    m_qlabel_trigger_source->setFixedSize(control_size);
    qgridLayout_left->addWidget(m_qlabel_trigger_source,6,0);

    m_qcombox_trigger_source = new QComboBox();
    m_qcombox_trigger_source->setFixedSize(control_size);
    m_qcombox_trigger_source->addItem("Line0");
    m_qcombox_trigger_source->addItem("Line1");
    m_qcombox_trigger_source->addItem("Line2");
    m_qcombox_trigger_source->addItem("Software");
    qgridLayout_left->addWidget(m_qcombox_trigger_source,7,0);


    m_qpushbutton_soft_triger = new QPushButton();
    m_qpushbutton_soft_triger->setFixedSize(control_size);
    m_qpushbutton_soft_triger->setText("软件触发");
    qgridLayout_left->addWidget(m_qpushbutton_soft_triger,8,0);
    connect(m_qpushbutton_soft_triger, &QPushButton::clicked, this, &CameraBaseQDialog::softTrigger);

    m_qpushbutton_save = new QPushButton();
    m_qpushbutton_save->setFixedSize(control_size);
    m_qpushbutton_save->setText("保存参数");
    qgridLayout_left->addWidget(m_qpushbutton_save,9,0);
    connect(m_qpushbutton_save, &QPushButton::clicked, this, &CameraBaseQDialog::saveParameter);

    m_qpushbutton_hide = new QPushButton();
    m_qpushbutton_hide->setFixedSize(control_size);
    m_qpushbutton_hide->setText("退出");
    qgridLayout_left->addWidget(m_qpushbutton_hide,10,0);

//    connect(m_qpushbutton_hide,&QPushButton::clicked,this,&CameraBaseQDialog::hide);
    qgridLayout->addLayout(qgridLayout_left,0,0);

    m_showqlable = new ShowQLabel(nullptr);
    m_showqlable->setFixedSize(1000,800);
    qgridLayout->addWidget(m_showqlable,0,1);


    connect(m_qtimer, &QTimer::timeout, this, [=]() {
//        m_bool_show_image = true;
        m_com.sendCom(m_camera_index,1);
        });

}

bool CameraBaseQDialog::initCamera(QString type,QString name,CameraCallback pfunction, void *pcontext)
{
    logger->info("initCamera");
//    QString m_exe_directory = QCoreApplication::applicationDirPath();
//    m_type = type;
//    QString filepath;
//    if(m_type=="hk")
//    {
//        filepath = QCoreApplication::applicationDirPath() + "/hk_camera.dll";
//    }
//    if(m_type=="dh")
//    {
//        filepath = QCoreApplication::applicationDirPath() + "/dh_camera.dll";
//    }




//    QLibrary* mainFunDll = new QLibrary(filepath);
//    bool state = mainFunDll->load();
//    if (state)
//    {

//        Fuction f = (Fuction) mainFunDll->resolve("newInstance");
//        if (f)
//            m_camera_interface = f();


//            if(m_camera_interface !=nullptr)
//            {
//                std::string string_name = name.toLocal8Bit().toStdString();
//                bool state = m_camera_interface->camera_init( (char*)string_name.c_str(),pfunction, pcontext);
//                if(state == false)
//                {
//                    return false;
//                }
//            }
//            else
//            {
//                return false;
//            }
//    }
//    else
//    {
//        return false;
//    }


    m_camera_interface = new DhCameraInterface();
    std::string string_name = name.toLocal8Bit().toStdString();
    bool state = m_camera_interface->camera_init( (char*)string_name.c_str(),pfunction, pcontext);
    if(state == false)
    {
        return false;
    }


    loadParameter();
    return true;
}

void CameraBaseQDialog::loadParameter()
{
//        int qspinbox_exposure_time =m_qsettings->value(m_section + "/" + "m_qspinbox_exposure_time", 0).toInt();
//        int qspinbox_gain = m_qsettings->value(m_section + "/" + "m_qspinbox_gain", 0).toInt();
        QString qcombox_trigger_mode = m_qsettings->value(m_section + "/" + "m_qcombox_trigger_mode", 0).toString();
        QString qcombox_trigger_source = m_qsettings->value(m_section + "/" + "m_qcombox_trigger_source", 0).toString();


//		m_qspinbox_exposure_time->setValue(m_qsettings->value(m_section + "/" + "m_qspinbox_exposure_time", 0).toInt());
//		m_qspinbox_gain->setValue(m_qsettings->value(m_section + "/" + "m_qspinbox_gain", 0).toInt());
//        m_qcombox_trigger_mode->setCurrentText(qcombox_trigger_mode);
        m_qcombox_trigger_source->setCurrentText(qcombox_trigger_source);




        bool state;



//            state = m_camera_interface->setExposureTime(m_qspinbox_exposure_time->value());
//            if(state==false)
//            {
//                    //QMessageBox::critical(nullptr, m_section, "ExposureTime");
//                    return;

//            }


//            state = m_camera_interface->setGainValue(m_qspinbox_gain->value());
//            if(state==false)
//            {
//                    //QMessageBox::critical(nullptr, m_section, "Gain");
//                    return;
//            }


//            state = m_camera_interface->setTriggerMode(m_qcombox_trigger_mode->currentText().toStdString());
            state = m_camera_interface->setTriggerMode("On");
            if(state==false)
            {
                    //QMessageBox::critical(nullptr, m_section, m_qcombox_trigger_mode->currentText());
                    return;
            }




            state = m_camera_interface->setTriggerSource(m_qcombox_trigger_source->currentText().toStdString());
            if(state==false)
            {
                    //QMessageBox::critical(nullptr, m_section, m_qcombox_trigger_source->currentText());
                    return;
            }
}

void CameraBaseQDialog::saveParameter()
{
    if(m_camera_interface == nullptr)
    {
        //QMessageBox::critical(nullptr, m_section, "相机初始化失败");
        return;
    }

    bool state;
//    state = m_camera_interface->setExposureTime(m_qspinbox_exposure_time->value());
//    if(state==false)
//    {
//        //QMessageBox::critical(nullptr, m_section, "setExposureTime");
//        return;
//    }

//    state = m_camera_interface->setGainValue(m_qspinbox_gain->value());
//    if(state==false)
//    {
//        //QMessageBox::critical(nullptr, m_section, "setGain");
//        return;
//    }


//    state = m_camera_interface->setTriggerMode(m_qcombox_trigger_mode->currentText().toStdString());
//    if(state==false)
//    {
//        QMessageBox::critical(nullptr, m_section, "setTriggerMode");
//        return;
//    }

    state = m_camera_interface->setTriggerSource(m_qcombox_trigger_source->currentText().toStdString());
    if(state==false)
    {
        QMessageBox::critical(nullptr, m_section, "trigger_source");
        return;
    }

//	m_qsettings->setValue(m_section + "/" + "m_qcombox_trigger_mode", m_qcombox_trigger_mode->currentText());
	m_qsettings->setValue(m_section + "/" + "m_qcombox_trigger_source", m_qcombox_trigger_source->currentText());


}

void CameraBaseQDialog::showImage(Mat image,int camera_index,int index)
{
//    if(m_bool_show_image==false)
//    {
//        return;
//    }
//    else
//    {
//        m_bool_show_image =false;
//    }

    QString message = m_type + " " + m_camera_name  + "  "  + QString::number(index);
    m_qlabel_message->setText(message);
    QImage q_image = matToQImage(image);
    if (!q_image.isNull())
    {

        m_showqlable->loadtextimage(q_image, message, Qt::green);
    }
}

void CameraBaseQDialog::softTrigger()
{
    if(m_camera_interface == nullptr)
    {
        return;
    }
    m_qtimer->start(50);


}
